home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmreduc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  4.6 KB  |  200 lines

  1. /* pbmreduce.c - read a portable bitmap and reduce it N times
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14.  
  15. void
  16. main( argc, argv )
  17.     int argc;
  18.     char* argv[];
  19.     {
  20.     FILE* ifp;
  21.     register bit** bitslice;
  22.     register bit* newbitrow;
  23.     register bit* nbP;
  24.     int argn, n, rows, cols, format, newrows, newcols;
  25.     int row, col, limitcol, subrow, subcol, count, direction;
  26.     char* usage = "[-floyd|-fs | -threshold] [-value <val>] N [pbmfile]";
  27.     int halftone;
  28. #define QT_FS 1
  29. #define QT_THRESH 2
  30. #define SCALE 1024
  31. #define HALFSCALE 512
  32.     long threshval, sum;
  33.     long* thiserr;
  34.     long* nexterr;
  35.     long* temperr;
  36.  
  37.     pbm_init( &argc, argv );
  38.  
  39.     argn = 1;
  40.     halftone = QT_FS;
  41.     threshval = HALFSCALE;
  42.  
  43.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  44.     {
  45.     if ( pm_keymatch( argv[argn], "-fs", 2 ) ||
  46.          pm_keymatch( argv[argn], "-floyd", 2 ) )
  47.         halftone = QT_FS;
  48.     else if ( pm_keymatch( argv[argn], "-threshold", 2 ) )
  49.         halftone = QT_THRESH;
  50.     else if ( pm_keymatch( argv[argn], "-value", 2 ) )
  51.         {
  52.         float f;
  53.  
  54.         ++argn;
  55.         if ( argn == argc || sscanf( argv[argn], "%f", &f ) != 1 ||
  56.          f < 0.0 || f > 1.0 )
  57.         pm_usage( usage );
  58.         threshval = f * SCALE;
  59.         }
  60.     else
  61.         pm_usage( usage );
  62.     ++argn;
  63.     }
  64.  
  65.     if ( argn == argc )
  66.     pm_usage( usage );
  67.     if ( sscanf( argv[argn], "%d", &n ) != 1 )
  68.     pm_usage( usage );
  69.     if ( n < 2 )
  70.     pm_error( "N must be greater than 1" );
  71.     ++argn;
  72.  
  73.     if ( argn == argc )
  74.     ifp = stdin;
  75.     else
  76.     {
  77.     ifp = pm_openr( argv[argn] );
  78.     ++argn;
  79.     }
  80.  
  81.     if ( argn != argc )
  82.     pm_usage( usage );
  83.  
  84.     pbm_readpbminit( ifp, &cols, &rows, &format );
  85.     bitslice = pbm_allocarray( cols, n );
  86.  
  87.     newrows = rows / n;
  88.     newcols = cols / n;
  89.     pbm_writepbminit( stdout, newcols, newrows, 0 );
  90.     newbitrow = pbm_allocrow( newcols );
  91.  
  92.     if ( halftone == QT_FS )
  93.     {
  94.     /* Initialize Floyd-Steinberg. */
  95.     thiserr = (long*) malloc( ( newcols + 2 ) * sizeof(long) );
  96.     nexterr = (long*) malloc( ( newcols + 2 ) * sizeof(long) );
  97.     if ( thiserr == 0 || nexterr == 0 )
  98.         pm_error( "out of memory" );
  99.  
  100.     srandom( (int) ( time( 0 ) ^ getpid( ) ) );
  101.     for ( col = 0; col < newcols + 2; ++col )
  102.         thiserr[col] = ( random( ) % SCALE - HALFSCALE ) / 4;
  103.         /* (random errors in [-SCALE/8 .. SCALE/8]) */
  104.     }
  105.     direction = 1;
  106.  
  107.     for ( row = 0; row < newrows; ++row )
  108.     {
  109.     for ( subrow = 0; subrow < n; ++subrow )
  110.         pbm_readpbmrow( ifp, bitslice[subrow], cols, format );
  111.  
  112.     if ( halftone == QT_FS )
  113.         for ( col = 0; col < newcols + 2; ++col )
  114.         nexterr[col] = 0;
  115.     if ( direction )
  116.         {
  117.         col = 0;
  118.         limitcol = newcols;
  119.         nbP = newbitrow;
  120.         }
  121.     else
  122.         {
  123.         col = newcols - 1;
  124.         limitcol = -1;
  125.         nbP = &(newbitrow[col]);
  126.         }
  127.  
  128.     do
  129.         {
  130.         sum = 0;
  131.         count = 0;
  132.         for ( subrow = 0; subrow < n; ++subrow )
  133.         for ( subcol = 0; subcol < n; ++subcol )
  134.             if ( row * n + subrow < rows && col * n + subcol < cols )
  135.             {
  136.             count += 1;
  137.             if ( bitslice[subrow][col * n + subcol] == PBM_WHITE )
  138.                 sum += 1;
  139.             }
  140.         sum = ( sum * SCALE ) / count;
  141.  
  142.         if ( halftone == QT_FS )
  143.         sum += thiserr[col + 1];
  144.  
  145.         if ( sum >= threshval )
  146.         {
  147.         *nbP = PBM_WHITE;
  148.         if ( halftone == QT_FS )
  149.             sum = sum - threshval - HALFSCALE;
  150.         }
  151.         else
  152.         *nbP = PBM_BLACK;
  153.  
  154.         if ( halftone == QT_FS )
  155.         {
  156.         if ( direction )
  157.             {
  158.             thiserr[col + 2] += ( sum * 7 ) / 16;
  159.             nexterr[col    ] += ( sum * 3 ) / 16;
  160.             nexterr[col + 1] += ( sum * 5 ) / 16;
  161.             nexterr[col + 2] += ( sum     ) / 16;
  162.             }
  163.         else
  164.             {
  165.             thiserr[col    ] += ( sum * 7 ) / 16;
  166.             nexterr[col + 2] += ( sum * 3 ) / 16;
  167.             nexterr[col + 1] += ( sum * 5 ) / 16;
  168.             nexterr[col    ] += ( sum     ) / 16;
  169.             }
  170.         }
  171.         if ( direction )
  172.         {
  173.         ++col;
  174.         ++nbP;
  175.         }
  176.         else
  177.         {
  178.         --col;
  179.         --nbP;
  180.         }
  181.         }
  182.     while ( col != limitcol );
  183.  
  184.     pbm_writepbmrow( stdout, newbitrow, newcols, 0 );
  185.  
  186.     if ( halftone == QT_FS )
  187.         {
  188.         temperr = thiserr;
  189.         thiserr = nexterr;
  190.         nexterr = temperr;
  191.         direction = ! direction;
  192.         }
  193.     }
  194.  
  195.     pm_close( ifp );
  196.     pm_close( stdout );
  197.  
  198.     exit( 0 );
  199.     }
  200.